<template>
  <div class="global-padding">
    <div style="margin-bottom: 10px;">
      <el-date-picker
        v-model="datePickerValue"
        type="daterange"
        align="right"
        unlink-panels
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="margin-right: 10px;"
        @change="datePickerConfirm"
      />
      <el-button type="primary" style="margin-left: 10px;" @click="conditionSearch">条件搜索</el-button>
      <el-button type="success" style="margin-left: 10px;" @click="addSwiper">添加轮播图</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="swiperId"
        label="ID"
        width="80"
      />
      <el-table-column
        fixed
        prop="createTime"
        label="日期"
      />
      <el-table-column
        prop="relArticleId"
        label="关联文章ID"
      />
      <el-table-column
        prop="priority"
        label="优先级"
      />
      <el-table-column
        label="轮播图"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="baseUrl + `/file/imgId/${scope.row.swiperImgId}`"
            :preview-src-list="[`${baseUrl}/file/imgId/${scope.row.swiperImgId}`]"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="面向群体"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.type == 0">面向全体</div>
          <div v-if="scope.row.type == 1">面向用户</div>
          <div v-if="scope.row.type == 2">面向司机</div>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="confirmDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加" :visible.sync="addDialogFormVisible">
      <el-form>
        <el-upload
          class="avatar-uploader"
          :action="baseUrl + '/file/upload'"
          :show-file-list="false"
          :headers="{'UT-TOKEN': token}"
          :on-success="handleAddUploadSuccess"
        >
          <img v-if="addSwiperImgId" :src="`${baseUrl}/file/imgId/${addSwiperImgId}`" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-form-item label="关联文章ID">
          <el-input v-model="addForm.relArticleId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="优先级">
          <el-input v-model="addForm.priority" autocomplete="off" />
        </el-form-item>
        <el-select v-model="addForm.type" placeholder="请选择面向群体" @change="selectOptionChange">
          <el-option
            label="全体"
            value="0"
          />
          <el-option
            label="用户"
            value="1"
          />
          <el-option
            label="司机"
            value="2"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="updateDialogFormVisible">
      <el-form>
        <el-upload
          class="avatar-uploader"
          :action="baseUrl + '/file/upload'"
          :show-file-list="false"
          :headers="{'UT-TOKEN': token}"
          :on-success="handleUploadSuccess"
        >
          <img v-if="swiperImgId" :src="`${baseUrl}/file/imgId/${swiperImgId}`" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-form-item label="关联文章ID">
          <el-input v-model="curRow.relArticleId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="优先级">
          <el-input v-model="curRow.priority" autocomplete="off" />
        </el-form-item>
        <el-select v-model="curRow.type" placeholder="请选择面向群体" @change="selectOptionChange">
          <el-option
            label="全体"
            value="0"
          />
          <el-option
            label="用户"
            value="1"
          />
          <el-option
            label="司机"
            value="2"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <div style="margin-top: 10px;text-align: center;">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="curPage"
        :page-size="pageSize"
        :total="total"
        @current-change="curPageChange"
      />
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import Cookies from 'js-cookie'
export default {
  data() {
    return {
      optionValue: '',
      curPage: 1,
      pageSize: 8,
      total: 1,
      tableData: [],
      dateField: 'create_time',
      datePickerValue: '',
      curRow: {},
      updateDialogFormVisible: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      swiperImgId: '',
      token: Cookies.get('token'),
      addDialogFormVisible: false,
      addSwiperImgId: '',
      addForm: {
        relArticleId: '',
        priority: '',
        type: ''
      }
    }
  },
  mounted() {
    this.reqListData()
  },
  methods: {
    handleUpdate(row) {
      console.log(row)
      this.curRow = row
      this.updateDialogFormVisible = true
      this.swiperImgId = row.swiperImgId
    },
    curPageChange(v) {
      console.log(v)
      this.curPage = v
      this.reqListData()
    },
    datePickerConfirm(v) {
      console.log(v)
      console.log(v[0])
    },
    selectOptionChange(v) {
      console.log(v)
    },
    reqListData() {
      request.get('swiper/swiperList', { params: {
        curPage: this.curPage,
        pageSize: this.pageSize,
        dateField: this.dateField,
        startDate: this.datePickerValue[0],
        endDate: this.datePickerValue[1]
      }}).then(res => {
        this.tableData = []
        console.log(res)
        for (const v of res.records) {
          v.gender = v.gender === 1 ? '男' : '女'
          v.isBan = v.isBan === 1 ? '已封禁' : '未封禁'
          this.tableData.push(v)
        }
        this.curPage = res.current
        this.pageSize = res.size
        this.total = res.total
      })
    },
    conditionSearch() {
      this.reqListData()
    },
    confirmDelete(row) {
      this.$alert('确定删除吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            request.delete(`/swiper/swiperDelete/${row.swiperId}`).then(res => {
              this.$message({
                type: 'info',
                message: `删除成功`
              })
              this.reqListData()
            })
          }
        }
      })
    },
    handleConfirmUpdate() {
      this.updateDialogFormVisible = false
      request.put(`/swiper/swiperUpdate`, {
        priority: this.curRow.priority,
        relArticleId: this.curRow.relArticleId,
        swiperId: this.curRow.swiperId,
        swiperImgId: this.swiperImgId,
        type: this.curRow.type
      }).then(res => {
        this.$message({
          type: 'info',
          message: `更新成功`
        })
        this.reqListData()
      })
    },
    handleUploadSuccess(res) {
      console.log(res)
      this.swiperImgId = res.data.data[0].imgId
    },
    handleAddUploadSuccess(res) {
      console.log(res)
      this.addSwiperImgId = res.data.data[0].imgId
    },
    addSwiper() {
      this.addDialogFormVisible = true
    },
    handleConfirmAdd() {
      request.post('swiper/swiperAdd', {
        'priority': this.addForm.priority,
        'relArticleId': this.addForm.relArticleId,
        'swiperImgId': this.addSwiperImgId,
        'type': this.addForm.type
      }).then(res => {
        console.log(res)
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
